<?php
/////////////////////////////////////////////////////
/// 函数名称：dh_static_gen 
/// 函数作用：产生静态的html页面
/// 函数作者: DH
/// 作者地址: http://dhblog.org/ 
/////////////////////////////////////////////////////

#需要使用的基础函数
include("dh_static_com.php");
#index页面的ajax分页
include("ajax_navi.php");
#得出分页的列表
include("page_navi.php");
#得出文章的目录
include("article_index.php");
#得出文章统计信息
include("stastics.php");
#得出文章的前后两篇文章
include("pre_next_bottom.php");

$DH_archivers_path= ABSPATH . 'static/';
$DH_archivers_src_path= ABSPATH . 'wp-content/themes/dhstatic/static/';
$DH_archivers_html_path= $DH_archivers_src_path . 'html/';
$DH_archivers_url= get_bloginfo('url'). '/static/';
//多长时间之内的文章需要更新
$DH_lastupdate = '2009-09-14 08:40:00';
//页面显示的文章列表条目的个数
$DH_page_count_limit = 4;
//分页中能展示的页面数
$DH_page_range = 6;
$DH_head = '';
$DH_side = '';
$DH_foot = '';

function dh_gen_static ()
{
	dh_global_set();
	dh_copy_files();
	update_index_post_list();
	dh_update_last();
}
add_action('publish_post', 'dh_gen_static');
//dh_gen_static();

function dh_global_set()
{
	global $DH_head,$DH_side,$DH_foot,$DH_archivers_html_path,$DH_archivers_url;
	
	$archivers_category_id = archivers_category_id(0);
	$archivers_tag = archivers_tag();
	$archivers_page = archivers_page();	
	
	$path_head_html  = $DH_archivers_html_path . 'head.html';
	$path_side_html  = $DH_archivers_html_path . 'side.html';
	$path_foot_html  = $DH_archivers_html_path . 'foot.html';

	$DH_head = dh_file_get_contents("$path_head_html");
	$DH_side = dh_file_get_contents("$path_side_html");
	$DH_foot = dh_file_get_contents("$path_foot_html");

	$DH_head = str_replace("%DH_category_contents%",$archivers_category_id,$DH_head);
	$DH_head = str_replace("%DH_home_href%",$DH_archivers_url.'index.html',$DH_head);
	$DH_head = str_replace("%DH_blogdescription%",get_option('blogdescription'),$DH_head);
	
	$DH_head = str_replace("%DH_blog_home%",$DH_archivers_url,$DH_head);
	$DH_head = str_replace("%DH_blog_name%",get_bloginfo('name'),$DH_head);
	
	$DH_side = str_replace("%DH_tag%",'讨论主题',$DH_side);
	$DH_side = str_replace("%DH_tag_contents%",$archivers_tag,$DH_side);
	$DH_side = str_replace("%DH_category%",'分类目录',$DH_side);	
	$DH_side = str_replace("%DH_side_single2%",'最热文章',$DH_side);
	$DH_side = str_replace("%DH_side_single2_content%",get_most_viewed_format('post',5,0,0,'<span class="title"><span class="smaller">►</span> ','</span>&nbsp;','','',' <span class="right_count">','</span>'),$DH_side);
	$DH_side = str_replace("%tag_widget%",'标签',$DH_side);
	$DH_side = str_replace("%tag_widget_content%",archivers_tag(),$DH_side);
	$DH_side = str_replace("%DH_side_double1%",'统计信息',$DH_side);
	$DH_side = str_replace("%DH_side_double1_content%",dh_get_site_stastics(),$DH_side);	
}

//拷贝style.css等静态文件到发布目录
function dh_copy_files()
{
	global $DH_archivers_path,$DH_archivers_src_path;
	if (!file_exists($DH_archivers_path))  
	{   
		mkdir($DH_archivers_path,0777);
	}
	if (!file_exists($DH_archivers_path.'images'))  
	{   
		mkdir($DH_archivers_path.'images',0777);
	}	
	if (!file_exists($DH_archivers_path.'css'))  
	{   
		mkdir($DH_archivers_path.'css',0777);
	}	
	if (!file_exists($DH_archivers_path.'js'))  
	{   
		mkdir($DH_archivers_path.'js',0777);
	}
	$copysrc = $DH_archivers_src_path . 'images/bg.png';
	$copydest = $DH_archivers_path . 'images/bg.png';
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'images/title_bg.jpg';
	$copydest = $DH_archivers_path . 'images/title_bg.jpg';
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'images/relog.swf';
	$copydest = $DH_archivers_path . 'images/relog.swf';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'images/postmeta.png';
	$copydest = $DH_archivers_path . 'images/postmeta.png';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'images/audio.png';
	$copydest = $DH_archivers_path . 'images/audio.png';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'css/style.css';
	$copydest = $DH_archivers_path . 'css/style.css';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'css/index.css';
	$copydest = $DH_archivers_path . 'css/index.css';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'css/single.css';
	$copydest = $DH_archivers_path . 'css/single.css';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'css/archive.css';
	$copydest = $DH_archivers_path . 'css/archive.css';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'js/ajax_navi.js';
	$copydest = $DH_archivers_path . 'js/ajax_navi.js';	
	copy($copysrc,$copydest);	
	$copysrc = $DH_archivers_src_path . 'js/article_index.js';
	$copydest = $DH_archivers_path . 'js/article_index.js';	
	copy($copysrc,$copydest);
	$copysrc = $DH_archivers_src_path . 'js/jquery.min.js';
	$copydest = $DH_archivers_path . 'js/jquery.min.js';	
	copy($copysrc,$copydest);	
}

//处理DH_lastupdate时间之类的文章
function dh_update_last()
{
	global $wpdb,$DH_lastupdate;
	$sql = "SELECT DISTINCT ID
		FROM $wpdb->posts
		LEFT JOIN $wpdb->comments ON ( $wpdb->posts.ID = $wpdb->comments.comment_post_ID ) 
		WHERE post_password = ''
		AND (post_type = 'post' or post_type = 'page' )
		AND post_status = 'publish'
		AND ( post_date > '$DH_lastupdate'
		OR post_modified > '$DH_lastupdate'
		OR comment_date > '$DH_lastupdate' )
		ORDER BY post_date DESC 
		";
	$update_ID = $wpdb->get_results($sql);

	if($update_ID) {
		foreach($update_ID as $post) {
			$post_ID = $post->ID;
			###update the post
			update_the_post($post->ID);
			###update post list
			//cat
			if(get_the_category($post->ID)){
				 foreach(get_the_category($post->ID) as $category) { 
					//echo DH_get_term_count($cat_id);
					update_cat_post_list($category->cat_ID);
				}
			}
			//tags
			if(get_the_tags($post_ID)){
				foreach(get_the_tags($post_ID) as $tag) {
					update_tag_post_list($tag->slug,$tag->term_id);
				 }
			}
		}
	} 
}

////处理index.html
function update_index_post_list()
{
	global $DH_archivers_path,$DH_page_count_limit,$DH_page_range,$DH_archivers_html_path,$DH_archivers_url,$wpdb,$DH_page_count_limit;
	//定义输入的index.html的模板的位置
	$path_index_html  = $DH_archivers_html_path . 'content_post_list.html';


	//打开文件，取出文件内容
	$index_html = dh_file_get_contents("$path_index_html");

	//$index_html = str_replace("%DH_lastest_post_contents%",archivers_lastest_post(0,10,1),$index_html);

	//替换固定字段
	
	$sql_rp = "SELECT ID, post_title,post_content,post_date
              FROM $wpdb->posts
              WHERE post_type = 'post'
              AND post_status = 'publish'
              AND post_password = ''
              ORDER BY ID DESC 
              LIMIT 0, $DH_page_count_limit";
	$get_recent_post = $wpdb->get_results($sql_rp);
	$index_html = str_replace("%DH_lastest_post_contents%",list_post($get_recent_post),$index_html);	
	$sql = "SELECT count(*) as count
            FROM $wpdb->posts
            WHERE post_type = 'post'
            AND post_status = 'publish'
            AND post_password = '';";
	$post_count = $wpdb->get_var($sql);
	$index_html = str_replace("%DH_post_list%",get_bloginfo('name'),$index_html);
	$index_html = str_replace("%DH_post_count%",$post_count,$index_html);
	$max_page = ceil((int)$post_count / (int)$DH_page_count_limit);
	update_option("dhstatic_max_page",$max_page);
	$link=get_option("home").'/?page=';
	$page_avi='<div class="page_navi">'.dh_pagenavi($DH_page_range,$max_page,$link,1).'</div>';	
	$index_html = str_replace("%DH_page_avi%",$page_avi,$index_html);
	global $DH_head,$DH_side,$DH_foot;	
	//index页面的头信息
	$DH_head_index = $DH_head;
	$DH_head_index = str_replace("%DH_blog_title%",get_bloginfo('name').'的首页',$DH_head_index);
	$DH_head_index = str_replace("%DH_keywords%",get_bloginfo('name').'的首页',$DH_head_index);
	$DH_head_index = str_replace("%DH_descriptions%",get_bloginfo('name').'的首页',$DH_head_index);
	$DH_head_index = str_replace("%cssfile%",'index.css',$DH_head_index);
	$index_js = '<script type="text/javascript" src="'.$DH_archivers_url.'js/ajax_navi.js"></script>';
	$DH_head_index = str_replace("%DH_script_js%",$index_js,$DH_head_index);
	
	$index_html = $DH_head_index.$index_html.$DH_side.$DH_foot;
	//定义输出的index.html的位置
	$file_index_html = $DH_archivers_path . 'index.html';
	dh_file_put_contents("$file_index_html","$index_html"); 	
}

###get the post_list
function update_cat_post_list($value) 
{
	global $wpdb,$DH_page_count_limit,$DH_page_range,$DH_archivers_path,$DH_archivers_html_path,$DH_archivers_url; 
	//首先更新其父catid
	$sql = "SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = $value AND taxonomy = 'category'";
	$catId_fathers = $wpdb->get_results($sql);
	if($catId_fathers)
	{
		foreach($catId_fathers as $catId_father)
		{
			update_cat_post_list($catId_father->parent);  
		}
	}
	
	$myposts = get_posts("category=$value&numberposts={$DH_page_count_limit}&post_status=publish&post_type=post"); 
	
	###options
	$DH_post_term = get_cat_name($value);
	$DH_post_term_permalink = get_category_link($value);
	###End options

	$path_post_list_html  = $DH_archivers_html_path.'content_post_list.html';
	$post_list_html = file_get_contents("$path_post_list_html");	
	
	$post_list_html = str_replace("%DH_lastest_post_contents%",list_post($myposts),$post_list_html);	

	$dhstatic_max_page = 'dhstatic_max_page_cat'.$value;
	$post_count = get_termid_count($value);
	$post_list_html = str_replace("%DH_post_list%",$DH_post_term,$post_list_html);
	$post_list_html = str_replace("%DH_post_count%",$post_count,$post_list_html);
		
	$max_page = ceil((int)$post_count / (int)$DH_page_count_limit);	
	update_option($dhstatic_max_page,$max_page);
	$link=get_option("home").'/?catid='.$value.'&cpage=';
	$page_avi='<div class="page_navi">'.dh_pagenavi($DH_page_range,$max_page,$link,1).'</div>';
	$post_list_html = str_replace("%DH_page_avi%",$page_avi,$post_list_html);
	global $DH_head,$DH_side,$DH_foot;
	//页面的头信息
	$DH_head_index = $DH_head;
	$DH_head_index = str_replace("%DH_blog_title%",$DH_post_term.'分类下的文章',$DH_head_index);
	$DH_head_index = str_replace("%DH_keywords%",$DH_post_term.'分类下的文章',$DH_head_index);
	$DH_head_index = str_replace("%DH_descriptions%",$DH_post_term.'分类下的文章',$DH_head_index);	
	$index_js = '<script type="text/javascript" src="'.$DH_archivers_url.'js/ajax_navi.js"></script>';
	$DH_head_index = str_replace("%cssfile%",'index.css',$DH_head_index);
	$DH_head_index = str_replace("%DH_script_js%",$index_js,$DH_head_index);
	
	$post_list_html = $DH_head_index.$post_list_html.$DH_side.$DH_foot;
	$file_index_html = $DH_archivers_path . 'cat'.$value.'.html';
	dh_file_put_contents("$file_index_html","$post_list_html"); 
}

function update_tag_post_list($value,$tagid) 
{
	global $wpdb,$DH_page_count_limit,$DH_page_range,$DH_archivers_path,$DH_archivers_html_path,$DH_archivers_url; 
	$myposts = get_posts("tag=$value&numberposts={$DH_page_count_limit}&post_status=publish&post_type=post");
	
	###options
	$DH_post_term = get_tag_name($tagid);
	###End options

	$path_post_list_html  = $DH_archivers_html_path.'content_post_list.html';
	$post_list_html = file_get_contents("$path_post_list_html");		
	$post_list_html = str_replace("%DH_lastest_post_contents%",list_post($myposts),$post_list_html);	
	$dhstatic_max_page = 'dhstatic_max_page_tag'.$tagid;
	$post_count=get_termid_count($tagid);
	$post_list_html = str_replace("%DH_post_list%",$DH_post_term,$post_list_html);
	$post_list_html = str_replace("%DH_post_count%",$post_count,$post_list_html);
	$max_page = ceil((int)$post_count / (int)$DH_page_count_limit);	
	update_option($dhstatic_max_page,$max_page);
	$link=get_option("home").'/?tagid='.$tagid.'&tpage=';
	$page_avi='<div class="page_navi">'.dh_pagenavi($DH_page_range,$max_page,$link,1).'</div>';	
	//echo $page_avi;
	$post_list_html = str_replace("%DH_page_avi%",$page_avi,$post_list_html);
	global $DH_head,$DH_side,$DH_foot;	
	//index页面的头信息
	$DH_head_index = $DH_head;
	$DH_head_index = str_replace("%DH_blog_title%",$DH_post_term.'分类下的文章',$DH_head_index);
	$DH_head_index = str_replace("%DH_keywords%",$DH_post_term.'分类下的文章',$DH_head_index);
	$DH_head_index = str_replace("%DH_descriptions%",$DH_post_term.'分类下的文章',$DH_head_index);		
	$index_js = '<script type="text/javascript" src="'.$DH_archivers_url.'js/ajax_navi.js"></script>';
	$DH_head_index = str_replace("%cssfile%",'index.css',$DH_head_index);
	$DH_head_index = str_replace("%DH_script_js%",$index_js,$DH_head_index);
	
	$post_list_html = $DH_head_index.$post_list_html.$DH_side.$DH_foot;	
	$file_index_html = $DH_archivers_path . 'tag'.$tagid.'.html';
	dh_file_put_contents("$file_index_html","$post_list_html"); 
}

//根据id刷新文章和分类、tag页面
function update_the_post($post_ID){
	global $wpdb,$DH_archivers_path,$DH_archivers_html_path,$DH_archivers_url;
	$sql_post = "SELECT ID, post_author, post_date, post_content, post_title
				 FROM $wpdb->posts 
				 WHERE ID = '$post_ID'";
	$get_update_post = $wpdb->get_results($sql_post);
	
	if($get_update_post)
	{
		foreach($get_update_post as $post)
		{
			$post_ID = $post->ID;
			$post_date = $post->post_date;
			$post_author = $post->post_author;
			$post_content_r = wpautop($post->post_content,1);
			//$post_content_r = article_index(pre_filter($post->post_content));
			$post_content_r = article_index($post_content_r);
			$DH_post_title = $post->post_title;
		}
	}
	else
	{
		return;
	}
    
    ###post category 
	$category = get_the_category($post_ID);
	$H_post_cats="";
	foreach($category as $categoryeach)
	{
		 $DH_post_cats = $DH_post_cats. ' , <a href=""' .$DH_archivers_url.'cat'.$categoryeach->cat_ID.'.html">'.$categoryeach->cat_name. '</a> ';
	}
	
    ###post tags
	$DH_post_tag ='';
	$DH_post_tag_k ='';
	if($get_the_tags = get_the_tags($id = $post_ID)) {
		foreach($get_the_tags as $tag) {
			$DH_post_tag = $DH_post_tag . get_the_tag_link($tag->term_id);
		}
		$DH_post_tag = rtrim($DH_post_tag,', '); 
		$DH_post_tag_k = strip_tags($DH_post_tag);
	}
	
	$DH_post_p = '发表时间: '. $post_date .'| 标签: '. get_the_tag_link($post_ID);
	
	$path_index_html  = $DH_archivers_html_path.'content_post.html';
	$post_html = file_get_contents("$path_index_html");	
	//$post_html = dh_replace_html($post_html);
	$post_html = str_replace("%DH_post_content%",$post_content_r,$post_html);
	$post_html = str_replace("%DH_post_title%",$DH_post_title,$post_html);
	$post_html = str_replace("%DH_post_p%",$DH_post_p,$post_html);
	$post_html = str_replace("%DH_post_cats%",get_the_category_link($post_ID),$post_html);
	$post_html = str_replace("%DH_post_buttom%",pre_next_bottom($post_ID),$post_html);
	
	global $DH_head,$DH_side,$DH_foot;
	//post 页面的头信息
	$DH_head_index = $DH_head;
	$DH_head_index = str_replace("%DH_blog_title%",$DH_post_title,$DH_head_index);
	$DH_head_index = str_replace("%DH_keywords%",$DH_post_title,$DH_head_index);
	$DH_head_index = str_replace("%DH_descriptions%",$DH_post_title,$DH_head_index);	
	$index_js = '<script type="text/javascript" src="'.$DH_archivers_url.'js/ajax_navi.js"></script>';
	$index_js = $index_js . '<script type="text/javascript" src="'.$DH_archivers_url.'js/content.js"></script>';
	$DH_head_index = str_replace("%DH_script_js%",$index_js,$DH_head_index);
	$post_html = $DH_head_index.$post_html.$DH_side.$DH_foot;
	$post_html = str_replace("%cssfile%",'single.css',$post_html);
	$file_post_html = $DH_archivers_path.'p'.$post_ID.'.html';	
	file_put_contents("$file_post_html","$post_html"); 
}

function list_post($post_in)
{
	global $DH_archivers_url; 	
	$post_out = '';
	foreach($post_in as $post) 
	{
		$recent_post_url = $DH_archivers_url.'p'.$post->ID.'.html'; 
		$u_time =strtotime($post->post_date);
		//echo $u_time;
		$post_out= $post_out.'
			<div class="post">
				<div class="postbox">
					<div class="postdate_top">' . date("y-m",$u_time) .'</div>
					<div class="postdate_bottom">
						<span class="DateD">'. date("d",$u_time) .'</span>
						<span class="DateW">'. date("D",$u_time) .'</span>
					</div>
					<div class="header">
						<span class="title"><a href="'. $recent_post_url .'" rel="bookmark" title="Permanent Link to'.$post->post_title.'">'.$post->post_title .'</a></span>
						<span class="comment" >阅读：<a href="recent_post_url" title="$post->post_title">'.post_views($post->ID).'</a></span>			
					</div>
					<div class="meta">
						<span class="category">分类：'.get_the_category_link($post->ID).'</span>
						<span class="tags">	标签：'.get_the_tag_link($post->ID).'</span>	
					</div>
					<div class="info">'.the_content_limit($post->post_content,515) .'</div>
				</div>
			</div>';
	}
	return $post_out;	
}
?>